﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Linq;
using System.Windows.Forms;
using HslCommunication.Core;

namespace HslCommunication.Controls
{
	/// <summary>
	/// 一个饼图的控件
	/// </summary>
	// Token: 0x02000177 RID: 375
	public class UserPieChart : UserControl
	{
		/// <summary>
		/// 实例化一个饼图的控件
		/// </summary>
		// Token: 0x06001EE7 RID: 7911 RVA: 0x00095978 File Offset: 0x00093B78
		public UserPieChart()
		{
			this.InitializeComponent();
			this.random = new Random();
			this.DoubleBuffered = true;
			this.formatCenter = new StringFormat();
			this.formatCenter.Alignment = StringAlignment.Center;
			this.formatCenter.LineAlignment = StringAlignment.Center;
			this.pieItems = new HslPieItem[0];
		}

		// Token: 0x06001EE8 RID: 7912 RVA: 0x00009AA1 File Offset: 0x00007CA1
		private void UserPieChart_Load(object sender, EventArgs e)
		{
		}

		/// <summary>
		/// 是否显示百分比信息
		/// </summary>
		// Token: 0x17000661 RID: 1633
		// (get) Token: 0x06001EE9 RID: 7913 RVA: 0x00095A10 File Offset: 0x00093C10
		// (set) Token: 0x06001EEA RID: 7914 RVA: 0x00095A28 File Offset: 0x00093C28
		[Browsable(true)]
		[Category("外观")]
		[DefaultValue(false)]
		[Description("获取或设置是否显示百分比占用")]
		public bool IsRenderPercent
		{
			get
			{
				return this.m_IsRenderPercent;
			}
			set
			{
				this.m_IsRenderPercent = value;
				base.Invalidate();
			}
		}

		/// <summary>
		/// 是否在图形上显示占比非常小的文本信息
		/// </summary>
		// Token: 0x17000662 RID: 1634
		// (get) Token: 0x06001EEB RID: 7915 RVA: 0x00095A3C File Offset: 0x00093C3C
		// (set) Token: 0x06001EEC RID: 7916 RVA: 0x00095A54 File Offset: 0x00093C54
		[Browsable(true)]
		[Category("外观")]
		[Description("获取或设置是否显示占比很小的文本信息")]
		[DefaultValue(true)]
		public bool IsRenderSmall
		{
			get
			{
				return this.m_IsRenderSmall;
			}
			set
			{
				this.m_IsRenderSmall = value;
				base.Invalidate();
			}
		}

		// Token: 0x06001EED RID: 7917 RVA: 0x00095A68 File Offset: 0x00093C68
		private void SetMarginPaint(int value)
		{
			bool flag = value > 500;
			if (flag)
			{
				this.margin = 80;
			}
			else
			{
				bool flag2 = value > 300;
				if (flag2)
				{
					this.margin = 60;
				}
				else
				{
					this.margin = 40;
				}
			}
		}

		// Token: 0x06001EEE RID: 7918 RVA: 0x00095AB0 File Offset: 0x00093CB0
		private Point GetCenterPoint(out int width)
		{
			bool flag = base.Width > base.Height;
			Point result;
			if (flag)
			{
				this.SetMarginPaint(base.Height);
				width = base.Height / 2 - this.margin;
				result = new Point(base.Height / 2 - 1, base.Height / 2 - 1);
			}
			else
			{
				this.SetMarginPaint(base.Width);
				width = base.Width / 2 - this.margin;
				result = new Point(base.Width / 2 - 1, base.Width / 2 - 1);
			}
			return result;
		}

		/// <summary>
		/// 随机生成颜色，该颜色相对于白色为深色颜色
		/// </summary>
		/// <returns></returns>
		// Token: 0x06001EEF RID: 7919 RVA: 0x00095B48 File Offset: 0x00093D48
		private Color GetRandomColor()
		{
			int num = this.random.Next(256);
			int num2 = this.random.Next(256);
			int blue = (num + num2 > 430) ? this.random.Next(100) : this.random.Next(200);
			return Color.FromArgb(num, num2, blue);
		}

		// Token: 0x06001EF0 RID: 7920 RVA: 0x00095BB0 File Offset: 0x00093DB0
		private void UserPieChart_Paint(object sender, PaintEventArgs e)
		{
			bool flag = !Authorization.nzugaydgwadawdibbas();
			if (!flag)
			{
				e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
				e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
				int num;
				Point centerPoint = this.GetCenterPoint(out num);
				Rectangle rectangle = new Rectangle(centerPoint.X - num, centerPoint.Y - num, num + num, num + num);
				bool flag2 = num > 0 && this.pieItems.Length != 0;
				if (flag2)
				{
					e.Graphics.FillEllipse(Brushes.AliceBlue, rectangle);
					e.Graphics.DrawEllipse(Pens.DodgerBlue, rectangle);
					Rectangle rect = new Rectangle(rectangle.X - centerPoint.X, rectangle.Y - centerPoint.Y, rectangle.Width, rectangle.Height);
					e.Graphics.TranslateTransform((float)centerPoint.X, (float)centerPoint.Y);
					e.Graphics.RotateTransform(90f);
					e.Graphics.DrawLine(Pens.DimGray, 0, 0, num, 0);
					int num2 = Enumerable.Sum<HslPieItem>(this.pieItems, (HslPieItem item) => item.Value);
					float num3 = 0f;
					float num4 = -90f;
					for (int i = 0; i < this.pieItems.Length; i++)
					{
						float num5 = 0f;
						bool flag3 = num2 == 0;
						if (flag3)
						{
							num5 = (float)(360 / this.pieItems.Length);
						}
						else
						{
							num5 = Convert.ToSingle((double)this.pieItems[i].Value * 1.0 / (double)num2 * 360.0);
						}
						using (Brush brush = new SolidBrush(this.pieItems[i].Back))
						{
							e.Graphics.FillPie(brush, rect, 0f, -num5);
						}
						e.Graphics.RotateTransform(0f - num5 / 2f);
						bool flag4 = num5 < 2f && !this.IsRenderSmall;
						if (flag4)
						{
							num3 += num5;
						}
						else
						{
							num3 += num5 / 2f;
							int num6 = 8;
							bool flag5 = num3 < 45f || num3 > 315f;
							if (flag5)
							{
								num6 = 15;
							}
							bool flag6 = num3 > 135f && num3 < 225f;
							if (flag6)
							{
								num6 = 15;
							}
							e.Graphics.DrawLine(Pens.DimGray, num * 2 / 3, 0, num + num6, 0);
							e.Graphics.TranslateTransform((float)(num + num6), 0f);
							bool flag7 = num3 - num4 < 5f;
							if (flag7)
							{
							}
							num4 = num3;
							bool flag8 = num3 < 90f;
							if (flag8)
							{
								e.Graphics.RotateTransform(num3 - 90f);
								e.Graphics.DrawLine(Pens.DimGray, 0, 0, this.margin - num6, 0);
								e.Graphics.DrawString(this.pieItems[i].Name, this.Font, Brushes.DimGray, new Point(0, -this.Font.Height));
								bool isRenderPercent = this.IsRenderPercent;
								if (isRenderPercent)
								{
									e.Graphics.DrawString(Math.Round((double)(num5 * 100f / 360f), 2).ToString() + "%", this.Font, Brushes.DodgerBlue, new Point(0, 1));
								}
								e.Graphics.RotateTransform(90f - num3);
							}
							else
							{
								bool flag9 = num3 < 180f;
								if (flag9)
								{
									e.Graphics.RotateTransform(num3 - 90f);
									e.Graphics.DrawLine(Pens.DimGray, 0, 0, this.margin - num6, 0);
									e.Graphics.DrawString(this.pieItems[i].Name, this.Font, Brushes.DimGray, new Point(0, -this.Font.Height));
									bool isRenderPercent2 = this.IsRenderPercent;
									if (isRenderPercent2)
									{
										e.Graphics.DrawString(Math.Round((double)(num5 * 100f / 360f), 2).ToString() + "%", this.Font, Brushes.DodgerBlue, new Point(0, 1));
									}
									e.Graphics.RotateTransform(90f - num3);
								}
								else
								{
									bool flag10 = num3 < 270f;
									if (flag10)
									{
										e.Graphics.RotateTransform(num3 - 270f);
										e.Graphics.DrawLine(Pens.DimGray, 0, 0, this.margin - num6, 0);
										e.Graphics.TranslateTransform((float)(this.margin - 8), 0f);
										e.Graphics.RotateTransform(180f);
										e.Graphics.DrawString(this.pieItems[i].Name, this.Font, Brushes.DimGray, new Point(0, -this.Font.Height));
										bool isRenderPercent3 = this.IsRenderPercent;
										if (isRenderPercent3)
										{
											e.Graphics.DrawString(Math.Round((double)(num5 * 100f / 360f), 2).ToString() + "%", this.Font, Brushes.DodgerBlue, new Point(0, 1));
										}
										e.Graphics.RotateTransform(-180f);
										e.Graphics.TranslateTransform((float)(8 - this.margin), 0f);
										e.Graphics.RotateTransform(270f - num3);
									}
									else
									{
										e.Graphics.RotateTransform(num3 - 270f);
										e.Graphics.DrawLine(Pens.DimGray, 0, 0, this.margin - num6, 0);
										e.Graphics.TranslateTransform((float)(this.margin - 8), 0f);
										e.Graphics.RotateTransform(180f);
										e.Graphics.DrawString(this.pieItems[i].Name, this.Font, Brushes.DimGray, new Point(0, -this.Font.Height));
										bool isRenderPercent4 = this.IsRenderPercent;
										if (isRenderPercent4)
										{
											e.Graphics.DrawString(Math.Round((double)(num5 * 100f / 360f), 2).ToString() + "%", this.Font, Brushes.DodgerBlue, new Point(0, 1));
										}
										e.Graphics.RotateTransform(-180f);
										e.Graphics.TranslateTransform((float)(8 - this.margin), 0f);
										e.Graphics.RotateTransform(270f - num3);
									}
								}
							}
							e.Graphics.TranslateTransform((float)(-(float)num - num6), 0f);
							e.Graphics.RotateTransform(0f - num5 / 2f);
							num3 += num5 / 2f;
						}
					}
					e.Graphics.ResetTransform();
				}
				else
				{
					e.Graphics.FillEllipse(Brushes.AliceBlue, rectangle);
					e.Graphics.DrawEllipse(Pens.DodgerBlue, rectangle);
					e.Graphics.DrawString("空", this.Font, Brushes.DimGray, rectangle, this.formatCenter);
				}
			}
		}

		/// <summary>
		/// 设置显示的数据源
		/// </summary>
		/// <param name="source">特殊的显示对象</param>
		/// <exception cref="T:System.ArgumentNullException"></exception>
		// Token: 0x06001EF1 RID: 7921 RVA: 0x0009639C File Offset: 0x0009459C
		public void SetDataSource(HslPieItem[] source)
		{
			bool flag = source != null;
			if (flag)
			{
				this.pieItems = source;
				base.Invalidate();
			}
		}

		/// <summary>
		/// 根据名称和值进行数据源的显示，两者的长度需要一致
		/// </summary>
		/// <param name="names">名称</param>
		/// <param name="values">值</param>
		/// <exception cref="T:System.ArgumentNullException"></exception>
		// Token: 0x06001EF2 RID: 7922 RVA: 0x000963C4 File Offset: 0x000945C4
		public void SetDataSource(string[] names, int[] values)
		{
			bool flag = names == null;
			if (flag)
			{
				throw new ArgumentNullException("names");
			}
			bool flag2 = values == null;
			if (flag2)
			{
				throw new ArgumentNullException("values");
			}
			bool flag3 = names.Length != values.Length;
			if (flag3)
			{
				throw new Exception("两个数组的长度不一致！");
			}
			this.pieItems = new HslPieItem[names.Length];
			for (int i = 0; i < names.Length; i++)
			{
				this.pieItems[i] = new HslPieItem
				{
					Name = names[i],
					Value = values[i],
					Back = this.GetRandomColor()
				};
			}
			base.Invalidate();
		}

		/// <summary> 
		/// 清理所有正在使用的资源。
		/// </summary>
		/// <param name="disposing">如果应释放托管资源，为 true；否则为 false。</param>
		// Token: 0x06001EF3 RID: 7923 RVA: 0x0009646C File Offset: 0x0009466C
		protected override void Dispose(bool disposing)
		{
			bool flag = disposing && this.components != null;
			if (flag)
			{
				this.components.Dispose();
				StringFormat stringFormat = this.formatCenter;
				if (stringFormat != null)
				{
					stringFormat.Dispose();
				}
			}
			base.Dispose(disposing);
		}

		/// <summary> 
		/// 设计器支持所需的方法 - 不要修改
		/// 使用代码编辑器修改此方法的内容。
		/// </summary>
		// Token: 0x06001EF4 RID: 7924 RVA: 0x000964B8 File Offset: 0x000946B8
		private void InitializeComponent()
		{
			base.SuspendLayout();
			base.AutoScaleDimensions = new SizeF(7f, 17f);
			base.AutoScaleMode = AutoScaleMode.Font;
			this.BackColor = Color.Transparent;
			this.Font = new Font("微软雅黑", 9f, FontStyle.Regular, GraphicsUnit.Point, 134);
			base.Margin = new Padding(3, 4, 3, 4);
			base.Name = "UserPieChart";
			base.Size = new Size(200, 200);
			base.Load += this.UserPieChart_Load;
			base.Paint += this.UserPieChart_Paint;
			base.ResumeLayout(false);
		}

		// Token: 0x04000703 RID: 1795
		private HslPieItem[] pieItems = new HslPieItem[0];

		// Token: 0x04000704 RID: 1796
		private Random random = null;

		// Token: 0x04000705 RID: 1797
		private StringFormat formatCenter = null;

		// Token: 0x04000706 RID: 1798
		private int margin = 40;

		// Token: 0x04000707 RID: 1799
		private bool m_IsRenderPercent = false;

		// Token: 0x04000708 RID: 1800
		private bool m_IsRenderSmall = true;

		/// <summary> 
		/// 必需的设计器变量。
		/// </summary>
		// Token: 0x04000709 RID: 1801
		private IContainer components = null;
	}
}
